package com.hz.ruoyistorage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hz.ruoyistorage.domain.LgColdStorage;
import com.hz.ruoyistorage.domain.LgGoods;
import com.hz.ruoyistorage.domain.mypojo.LgGoodsSome;
import com.hz.ruoyistorage.mapper.LgGoodsMapper;
import com.hz.ruoyistorage.mapper.LgGoodsSomeMapper;
import com.hz.ruoyistorage.service.LgGoodsSomeService;
import com.hz.ruoyistorage.utils.ResultJSON;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Author gj
 * @Date 2025/3/10 16:03
 * @注释
 */
@RestController
@RequestMapping("/goods")
public class LgGoodsSomeController {
    @Autowired
    private LgGoodsSomeService lgGoodsSomeService;
    @Autowired
    private LgGoodsSomeMapper lgGoodsSomeMapper;
    @Autowired
    private LgGoodsMapper lgGoodsMapper;

    //获得父类
//    @RequiresPermissions("storage:goods:getAll")
    @RequestMapping("/getParent")
    public ResultJSON getParent(){
        return ResultJSON.success(lgGoodsSomeService.getParent());
    }
    /**
    * @Description: app用
    * @Param: [id]
    * @return: com.hz.ruoyistorage.utils.ResultJSON
    * @Author: grx
    * @Date: 15:23 2025/3/13
    */
//    @RequiresPermissions("storage:goods:getAll")
    @RequestMapping("/getGoodsByParentId")
    public ResultJSON getGoodsByParentId(Integer id){
        QueryWrapper<LgGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        queryWrapper.eq("delete_status",0);
        return ResultJSON.success(lgGoodsMapper.selectList(queryWrapper));
    }

    //获得子类
//    @RequiresPermissions("storage:goods:getAll")
    @RequestMapping("/getChild")
    public ResultJSON getChild(@RequestBody Map<String,Object> query){
        return ResultJSON.success(lgGoodsSomeService.getChild(query));
    }
    //新增
//    @RequiresPermissions("storage:goods:add")
    @RequestMapping("/addGoods")
    public ResultJSON addGoods(@RequestBody LgGoodsSome lgGoodsSome){
        return ResultJSON.success(lgGoodsMapper.insert(lgGoodsSome));
    }
    //新增大类
//    @RequiresPermissions("storage:goods:add")
    @RequestMapping("/addGoods2")
    public ResultJSON addGoods2(@RequestBody LgGoodsSome lgGoodsSome){
        return ResultJSON.success(lgGoodsMapper.insert(lgGoodsSome));
    }
    //获得一个
//    @RequiresPermissions("storage:goods:getAll")
    @RequestMapping(value = "/{goodsId}")
    public ResultJSON getOne(@PathVariable Integer goodsId){
        return ResultJSON.success(lgGoodsMapper.selectById(goodsId));
    }
    //修改
//    @RequiresPermissions("storage:goods:update")
    @RequestMapping("/updateGoods")
    public ResultJSON updateGoods(@RequestBody LgGoodsSome lgGoodsSome){
        return ResultJSON.success(lgGoodsMapper.updateById(lgGoodsSome));
    }
    //获得条数
//    @RequiresPermissions("storage:goods:getAll")
    @RequestMapping("/getTotal")
    public ResultJSON getTotal(@RequestBody Map<String,Object> query){
        return ResultJSON.success(lgGoodsSomeMapper.getTotal(query));
    }
    //导出
    @RequiresPermissions("storage:goods:export")
    @Log(title = "大冷库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LgGoods lgGoods)
    {

        List<LgGoods> list = lgGoodsMapper.selectList(null);
        ExcelUtil<LgGoods> util = new ExcelUtil<LgGoods>(LgGoods.class);
        util.exportExcel(response, list, "冷库数据");
    }
//    @RequiresPermissions("storage:goods:delete")
    @PostMapping(value = "/{goodsIds}")
    public ResultJSON deleteGoods(@PathVariable Long[] goodsIds){
        for (Long goodsId : goodsIds){

            lgGoodsMapper.deleteById(goodsId);
        }
        return ResultJSON.success();
    }

//    @RequiresPermissions("storage:goods:delete")
    @GetMapping(value = "/del/{goodsIds}")
    public ResultJSON delGoods(@PathVariable Integer goodsIds){
        System.out.println("---------------------------"+goodsIds);
        return ResultJSON.success(lgGoodsMapper.delGoods(goodsIds));
    }
}
